#include "common/assert.h"
#include "common/exception.h"
#include "string/string.h"
#include "string/string_util.h"

#include <stdio.h>

void print(const String& s, const String& name)
{
	printf("Printing string %s\n", name.c_str());

	for (int i = 0; i < s.length(); ++i)
	{
		printf("s[%d]: %c\n", i, s[i]);
	}
	
	printf("\n");

}

void test_string()
{
	String s("Hello world!");

	printf("It's got the c string %s\n", s.c_str());
	printf("And chars:\n");

	print(s, "s");

	String t("Hello world!");

	ASSERT(s == t);

	t[0] = 'T';

	ASSERT(s != t);
	ASSERT(t == "Tello world!");

	t.length(15);

	print(t, "t");

	String u("abcd");
	u.length(8, 'x');
	ASSERT(u == "abcdxxxx");
	ASSERT(u.length() == 8);

	print(u, "u");

	u.length(2);
	ASSERT(u.length() == 2);

	print(u, "u");
}

void test_string_util()
{
	String result = fmt("%s %s", String("Foobar").c_str(), String("Barfoo").c_str());
	ASSERT(result == "Foobar Barfoo");
	ASSERT(result.length() == 13);

	result = fmt("", 1, 2, 3);
	ASSERT(result == "");
	ASSERT(result.length() == 0);

	result = fmt("%s%s%s", "", "", String("").c_str());
	ASSERT(result == "");
	ASSERT(result.length() == 0);

}

int string_test()
{
	try
	{
		test_string();
		test_string_util();
	} catch (const Exception& exception)
	{
		printf(exception.message());
		getchar();
	}

	getchar();
	return 0;
}